<?php
/**
 * @author Nguyễn Sơn Tùng <sontung.x305@gmail.com> <Y!M: terminator_nst> <Skype : joker.x305>
 * @version: 1.0
 * @copyright fteam.vn
 * Date: 21/01/2013
 * Time: 18:18
 */

class fSlug extends CWidget {
    public $model = null;
    public $source = 'name';
    public $target = 'slug';

    public function init(){
        if($this->model !== null){
            $this->source = get_class($this->model).'_'.$this->source;
            $this->target = get_class($this->model).'_'.$this->target;
        }
    }

    private function publicAssets(){
        $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
        $baseUrl = Yii::app()->getAssetManager()->publish($dir);
        $cs = Yii::app()->clientScript;
        $cs->registerCoreScript('jquery',2);
        $cs->registerScriptFile($baseUrl.'/string.toSlug.js',2);
        $cs->registerScriptFile($baseUrl.'/fslug.js',2);
        $script = "$('#{$this->source}').fSlug({target:'#{$this->target}'});";
        $cs->registerScript($this->target,$script, 2);
    }
    public function run(){
        $this->publicAssets();
    }
}